package cn.xkai.exercise.c;

/**
 * @description: x 的平方根
 * @author: kaixiang
 * @date: 2022-07-29
 **/
public class Solution113 {
    public static int mySqrt(int x) {
        if (x < 2) {
            return x;
        }
        int left = 1, right = x;
        while (left <= right) {
            int mid = (left + right) >>> 1;
            if (x / mid == mid) {
                return mid;
            } else if (x / mid < mid) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }

    public static void main(String[] args) {
        int x = 6;
        System.out.println(mySqrt(x));
        for (int i = 0; i < 10; i++) {
            System.out.println(mySqrt(i));
        }
    }
}
